package com.share.service.oa.org.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.share.bean.oa.org.EmpCard;
import com.share.dao.oa.org.EmpCardDao;
import com.share.dao.oa.org.EmployeeDao;
import com.share.service.oa.org.EmpCardManager;

/**
 * 说明：员工卡信息
 * <br/>作者：杨浩泉
 * <br/>日期：2010-6-28
 */
@Service
public class EmpCardManagerImpl implements EmpCardManager{
	
	@Resource
	private EmpCardDao ecd ;
	
	@Resource
	private EmployeeDao empDao ;

	public EmpCardDao getEcd() {
		return ecd;
	}

	public void setEcd(EmpCardDao ecd) {
		this.ecd = ecd;
	}

	@Override
	public void saveEmpCardManager(EmpCard ec,String empNocardId) throws Exception {
		
		Serializable retId = this.ecd.saveEmpCardDao(ec) ;
		
		if(retId != null && !"".equals(retId)) {
			
			String hql = "update Employee o" ;
			StringBuffer setHQL = new StringBuffer() ;
			StringBuffer whereHQL = new StringBuffer() ;
			
			setHQL.append("card_id=?") ;
			whereHQL.append("o.empNo=?") ;
			
			this.empDao.updateByParamEmpDao(hql, setHQL.toString(), whereHQL.toString(), new Object[]{retId,empNocardId.split(",")[0]});
			
		}
	}

	@Override
	public EmpCard loadEmpCardManager(String empNocardId) throws Exception {
		
		String[] empStr = empNocardId.split(",") ;
		
		String hql = "select o from EmpCard o" ;
		
		StringBuffer whereHQL = new StringBuffer() ;
		whereHQL.append("o.id=?") ;
		
		List<Object> param = new ArrayList<Object>() ;
		param.add((empStr.length >= 2 ? Integer.parseInt(empStr[1]) : 0)) ;
		
		
		return this.ecd.loadEmpCardDao(hql, whereHQL.toString(), param) ;
	}
	
	@Override
	public void updateEmpCardManager(EmpCard ec) throws Exception {
		this.ecd.updateEmpCardDao(ec) ;
	}

}
